package com.wing.res.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.enums.MaintenanceEnum;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.res.model.response.ResProTaskSearchVo;
import com.wing.res.service.*;

import com.wing.sell.model.entity.OrderProEnclosure;
import com.wing.sell.service.OrderProEnclosureService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * 质检项目-Controller
 *
 * @Author xiayang
 **/
@Slf4j
@Validated
@Api(value = "Bom规划-bom任务", tags = "#/res/planning/bomPlan/bomTask")
@RestController("resBomPlanProjectController")
@RequestMapping("qualityProject/admin/bomPlanProject")
public class ResBomPlanProjectController {

    @Autowired
    private ResProTaskService resProTaskService;

    @Autowired
    private OrderProEnclosureService orderProEnclosureService;

    @Autowired
    private ResProBomRecordService resProBomRecordService;

    @ApiOperation(value = "查研发任务列表", notes = "搜索-任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ResProTaskSearchVo> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,Long productTypeId
            ,Long customerId
            ,String customerProCode
            ,String productStatus
            ,String customerProFigure
            ,String givenStartTimed
    ) {
        Page<ResProTaskSearchVo> pg = new Page<>(page, limit);
        Map<String,Object> map = new HashMap<>();
        map.put("customerId", customerId);
        map.put("customerProCode", customerProCode);
        map.put("productStatus", productStatus);
        if (StrUtil.isNotEmpty(givenStartTimed)){
            // 使用 split 方法分割字符串
            String[] dates = givenStartTimed.split(" 至 ");
            // 获取开始和结束日期
            String startDate = dates[0];
            String endDate = dates[1];
            map.put("givenStartTime", startDate);
            map.put("givenEndTime", endDate);
        }
        map.put("customerProFigure", customerProFigure);
        map.put("status", MaintenanceEnum.Task_Status_Enum.DQR.getCode());
        map.put("productTypeId", productTypeId);
        String noStatus = "";
        Page<ResProTaskSearchVo> list = resProTaskService.pageList4app(pg, map, noStatus);
        list.getRecords().forEach(info-> {
            info.setProductStatus(MaintenanceEnum.Task_Status_Enum.getNameByCode(info.getProductStatus()));
            info.setSource(MaintenanceEnum.Task_Source_Enum.getNameByCode(info.getSource()));
            QueryWrapper<OrderProEnclosure> enclosureQueryWrapper = new QueryWrapper<>();
            enclosureQueryWrapper.eq("task_id",info.getId());
            OrderProEnclosure one = orderProEnclosureService.getOne(enclosureQueryWrapper);
            if (ObjectUtil.isNotEmpty(one)){
                info.setFileUrl(one.getFileUrl());
                info.setCusProductFigure(one.getFileName());
            }
        });
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    /**
     * 确认
     * @param id
     * @return
     */
    @ApiOperation(value = "点击确认", notes = "确认")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/confirm/{id}")
    public JsonResult confirm(@PathVariable("id") String id) {
        if (resProBomRecordService.confirm(id) > 0) {
            return JsonResult.success("操作成功");
        }
        return JsonResult.fail("操作失败");
    }

}
